﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Win.Controls;
using ESRI.ArcGIS.Geodatabase;
using PlanningGIS.ArcGIS;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 要素类集合控件，只能选择要素类对象
    /// </summary>
    public partial class CtlDataSetList : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CtlDataSetList()
        {
            InitializeComponent();            
        }

        /// <summary>
        /// 选择改变事件
        /// </summary>
        public event EventHandler SelectChanged = null;

        /// <summary>
        /// 获取选择的数据对象
        /// </summary>
        public List<IFeatureClass> Datasets
        {
            get
            {
                if (lstDataset.Rows.Count == 0) return null;
                List<IFeatureClass> nodes = new List<IFeatureClass>();
                foreach (DataGridViewRow itm in lstDataset.Rows)
                {
                    nodes.Add(itm.Tag as IFeatureClass);
                }
                return nodes;
            }
        }

        /// <summary>
        /// 已添加到列表中的图层所对应的空间参考
        /// </summary>
        public List<ISpatialReference> SpatialRefs { get; private set; }

        /// <summary>
        /// 点击菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuBtns_ItemClick(object sender, EventArgs e)
        {
            RadialMenuItem item = sender as RadialMenuItem;
            if (item == null) return;
            if (item == mnuAdd)
            {
                Add();
            }
            else if (item == mnuRemove)
            {
                Remove();
            }
            else if (item == mnuClear)
            {
                Clear();
            }            
        }

        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {
            OpenDataDialog dlg = new OpenDataDialog();
            dlg.MultiSelect = true;
            if (dlg.ShowDialog(this) != DialogResult.OK) return;            
            List<IDataNode> nodes = dlg.GetSelectNode<IDataNode>();
            if (nodes == null || nodes.Count == 0) return;
            this.Cursor = Cursors.WaitCursor;
            try
            {
                foreach (IDataNode nd in nodes)
                {
                    List<IFeatureClass> fs = nd.OpenFeatureClasses();
                    if (fs == null || fs.Count == 0) continue;
                    foreach (IFeatureClass f in fs)
                    {
                        AddItem(f);
                    }
                }
                if (SelectChanged != null) SelectChanged(this, new EventArgs());
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// 添加子项到数据列表中
        /// </summary>
        /// <param name="node"></param>
        private void AddItem(dynamic node)
        {
            string name = "";
            string path = "";
            if (node is IDataNode)
            {
                name = ((IDataNode)node).Name;
                path = ((IDataNode)node).Path;
            }
            else if (node is IFeatureClass)
            {
                name = ((IFeatureClass)node as IDataset).Name;
                path = ((IFeatureClass)node).GetFullName();
                ISpatialReference sp = ((IFeatureClass)node as IDataset as IGeoDataset).SpatialReference;
                if (SpatialRefs == null) SpatialRefs = new List<ISpatialReference>();
                ISpatialReference smsp = SpatialRefs.FirstOrDefault(p => p.FactoryCode == sp.FactoryCode);
                if (smsp == null) SpatialRefs.Add(sp);
            }
            int idx = lstDataset.Rows.Add();
            DataGridViewRow itm = lstDataset.Rows[idx];
            itm.Cells[clnName.Index].Value = name;
            itm.Cells[clnPath.Index].Value = path;
            itm.Tag = node;
        }

        /// <summary>
        /// 移除选择的项
        /// </summary>
        private void Remove()
        {
            if (lstDataset.SelectedRows.Count == 0) return;
            foreach (DataGridViewRow itm in lstDataset.SelectedRows)
            {
                lstDataset.Rows.Remove(itm);
                if (itm.Tag != null && itm.Tag is IFeatureClass)
                {
                    if (SpatialRefs != null && SpatialRefs.Count > 0)
                    {
                        ISpatialReference sp = (itm.Tag as IFeatureClass as IDataset as IGeoDataset).SpatialReference;
                        if (SpatialRefs.Contains(sp)) SpatialRefs.Remove(sp);
                    }                    
                }
            }
            if (SelectChanged != null && lstDataset.Rows.Count != 0) SelectChanged(this, new EventArgs());
        }

        /// <summary>
        /// 清除
        /// </summary>
        private void Clear()
        {
            if (lstDataset.Rows!=null && lstDataset.Rows.Count != 0)
            {
                lstDataset.Rows.Clear();
                if (SpatialRefs != null) SpatialRefs.Clear();
                SpatialRefs = new List<ISpatialReference>();
                if (SelectChanged != null) SelectChanged(this, new EventArgs());
            }
        }
    }
}
